package com.zpark.controller;

import com.zpark.pojo.BasketballMallUser;
import com.zpark.service.IUserService;
import com.zpark.pojo.BasketballMallUser;
import com.zpark.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户控制层
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 注册接口
     * @param user
     * @return
     */
    @PostMapping("/reg")
    public ModelAndView reg(BasketballMallUser user){
        // 创建视图解析器对象
        ModelAndView mv = new ModelAndView();
        // 设置视图解析
        mv.setViewName("login");

        // 调用业务层方法执行注册功能
        userService.addUser(user);

        return mv;
    }

    /**
     * 访问注册页面接口
     * @return
     */
    @RequestMapping("/reg_page")
    public ModelAndView regPage(){
        // 创建视图解析器对象
        ModelAndView mv = new ModelAndView();
        // 设置视图解析
        mv.setViewName("register");

        return mv;
    }

    @PostMapping("user_login")
    public ModelAndView login(String uname, String pwd, HttpServletRequest request){
        //创建视图解析器
        ModelAndView mv = new ModelAndView();
        //设置视图解析
        mv.setViewName("index");

        //调用业务层方法执行登录
        BasketballMallUser user = new BasketballMallUser();

        //将登录信息保存到session域中
        HttpSession session = request.getSession();
        session.setAttribute("User");

        return mv;
    }
}
